#include "BaseThread.h"

BaseThread::BaseThread(QObject *parent) : QThread(parent) {}

BaseThread::~BaseThread() {}

void BaseThread::resume() {
	qDebug() << QThread::currentThreadId() << " resume...";
	if (QThread::isRunning()) {
		this->m_pauseFlag = false;
		m_mutex.lock();
		m_condition.wakeAll();
		m_mutex.unlock();
	}

}

void BaseThread::pause(int timeout) {
	if (QThread::isRunning()) {
		qDebug() << QThread::currentThreadId() << " pause...";
		//暂停线程
		this->m_pauseFlag = true;
		m_mutex.lock();
		if (timeout > 0) {
			m_condition.wait(&m_mutex, timeout);
		} else {
			m_condition.wait(&m_mutex);
		}
		m_mutex.unlock();
	}
}

void BaseThread::stop() { 
	qDebug() << QThread::currentThreadId() << " stop...";
	this->m_stopFlag = true;
	m_mutex.lock();
	m_condition.wakeAll();
	m_mutex.unlock();
	this->quit();
	this->wait();
}

void BaseThread::start(Priority pri) { 
	this->m_stopFlag = false;
	this->m_pauseFlag = false;
	QThread::start(pri);
}

bool BaseThread::isRunning() {
	return !this->m_pauseFlag && !this->m_stopFlag && QThread::isRunning();
}

bool BaseThread::isHangUp() {
	return this->m_pauseFlag && QThread::isRunning();
}

bool BaseThread::isStop() {
	return this->m_stopFlag || !QThread::isRunning();
}
